package com.zego.wawaji.ui.store.convert;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.log.PToast;
import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.CustomFrameLayout;
import com.zego.wawaji.ui.center.address.bean.Address;
import com.zego.wawaji.ui.center.address.bean.AddressList;
import com.zego.wawaji.ui.store.bean.Store;
import com.zego.wawaji.wawaji_client.util.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品兑换
 * Created by Su on 2018/1/9.
 */
public class StoreConvertActivity extends BaseAppCompatActivity implements RequestComplete {
    private CustomFrameLayout customFrame;
    private TextView tvName, tvAddress, tvGoldNum;
    private RecyclerView recyclerView;
    private ConvertAdapter convertAdapter;

    private Store store;
    private int storeNum = 1;
    private boolean haveAddr = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_convert_activity);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        reqData();
    }

    private void reqData() {
        ModuleMgr.getCenterMgr().reqGetAddress(this);
    }

    private void initView() {
        setBackView();
        setTitle("填写订单");
        store = getIntent().getParcelableExtra("store");
        if (store == null) store = new Store();

        initAddress();
        tvGoldNum = (TextView) findViewById(R.id.tv_gold_num);
        tvGoldNum.setText(String.valueOf(store.getWwCoinCz()));

        findViewById(R.id.btn_change).setOnClickListener(listener);
        findViewById(R.id.address_item).setOnClickListener(listener);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        convertAdapter = new ConvertAdapter();
        recyclerView.setAdapter(convertAdapter);

        List<Store> data = new ArrayList<>();
        data.add(store);
        convertAdapter.setList(data);

        convertAdapter.setOnChaneNum(new ConvertAdapter.OnChangeNum() {
            @Override
            public void onNum(int num) {
                storeNum = num;
                tvGoldNum.setText(String.valueOf(store.getWwCoinCz() * num));
            }
        });
    }

    private void initAddress() {
        tvName = (TextView) findViewById(R.id.tv_name);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        customFrame = (CustomFrameLayout) findViewById(R.id.customFrame);
        customFrame.show(R.id.tv_no_data);
    }

    private void fillAddress(Address address) {
        if (address == null) return;
        tvName.setText(address.getReceiverName());
        tvAddress.setText(address.getProvince() + " " + address.getCity() + " " + address.getStreet());
    }

    private NoDoubleClickListener listener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.btn_change:
                    if (store == null) {
                        PToast.showShort("商品信息获取失败，请重试");
                        return;
                    }
                    if (!haveAddr) {
                        PToast.showShort("请选择发货地址");
                        return;
                    }
                    ModuleMgr.getWawaMgr().reqGoodsExchange(store.getId(), storeNum, new RequestComplete() {
                        @Override
                        public void onRequestComplete(HttpResponse response) {
                            if (response.isOk()) {
                                finish();
                            }
                        }
                    });
                    break;

                case R.id.address_item:
                    UIShow.showMyAddressAct(StoreConvertActivity.this);
                    break;
            }
        }
    };

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqGetAddress) {
            if (!response.isOk()) {
                return;
            }

            AddressList dataList = (AddressList) response.getBaseData();
            if (dataList == null || dataList.getAddressList().size() <= 0) {
                haveAddr = false;
                return;
            }

            haveAddr = true;
            customFrame.show(R.id.ll_address);
            for (Address address : dataList.getAddressList()) {
                if (address.isDefault()) {
                    fillAddress(address);
                    return;
                }
            }
        }
    }
}
